#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author  : 邢建辉
# @Email   : xjh_0125@sina.com
# @Time    : 2022/1/12 10:12
# @Software: PyCharm
# @File    : l1669_merge_in_between.py


# Definition for singly-linked list.

from data_structure.list_node import ListNode, gen_link, print_link


class Solution:
    def mergeInBetween_val(self, list1: ListNode, a: int, b: int, list2: ListNode) -> ListNode:
        head_1 = list1
        head_2 = list2
        while True:
            if head_1.next.val == a:
                next_1 = head_1.next
                head_1.next = head_2
                while next_1.val != b:
                    next_1 = next_1.next
                while head_2.next:
                    head_2 = head_2.next
                head_2.next = next_1.next
                return list1
            else:
                head_1 = head_1.next

    def mergeInBetween(self, list1: ListNode, a: int, b: int, list2: ListNode) -> ListNode:
        head_1 = list1
        head_2 = list2
        i, j = 0, a
        while True:
            if i == a - 1:
                next_1 = head_1.next
                head_1.next = head_2
                while j < b:
                    j += 1
                    next_1 = next_1.next
                while head_2.next:
                    head_2 = head_2.next
                head_2.next = next_1.next
                return list1
            else:
                head_1 = head_1.next
                i += 1


if __name__ == '__main__':
    s = Solution()
    list1 = [0, 1, 2, 3, 4, 5, 6]
    list2 = [1000000, 1000001, 1000002, 1000003, 1000004]
    a = 2
    b = 5
    print_link(s.mergeInBetween(gen_link(list1, 0), a, b, gen_link(list2, 0)))
